<!DOCTYPE html>

<html lang="zh-CN">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>回调函数</title>
</head>

<body>

</body>
<script>
    function ajaxFunction() {
        var xhr;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } else {
            xhr = ActiveXObject("MiscroSoft.XMLHTTP");
        }
        return xhr;
    }

    function ajax(url, method, data, callback) {
        var xmlHttp = ajaxFunction();
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200 || xmlHttp.status == 304) {
                    callback();
                }
            }
        }
        xmlHttp.open(method, url, true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.send("data=" + data);
    }

    ajax('http://www.baidu.com', "GET", null, function () {
        alert('ok');
    })
</script>

</html>